package com.example.storage;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class SqLiteActivity extends AppCompatActivity {

    DBHelper  dbHelper = new DBHelper(this,"user", null, 1);
    private SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sq_lite);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        ((Button) findViewById(R.id.SQLite_add)).setOnClickListener(v -> {
            if(     !((EditText) findViewById(R.id.SQLite_name)).getText().toString().isEmpty() &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().isEmpty() ||
                    !((EditText) findViewById(R.id.SQLite_name)).getText().toString().equals("") &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().equals("")
            ){
              dbHelper.insert(((EditText) findViewById(R.id.SQLite_name)).getText().toString(),((EditText) findViewById(R.id.SQLite_phone)).getText().toString());
                ((TextView) findViewById(R.id.SQLite_result)).setText("添加成功\nusername:"+((EditText) findViewById(R.id.SQLite_name)).getText().toString()+"\nphone:"+((EditText) findViewById(R.id.SQLite_phone)).getText().toString());
            }else {
                Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
            }


        });
        ((Button) findViewById(R.id.SQLite_query)).setOnClickListener(v -> {
            if(     !((EditText) findViewById(R.id.SQLite_name)).getText().toString().isEmpty() &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().isEmpty() ||
                    !((EditText) findViewById(R.id.SQLite_name)).getText().toString().equals("") &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().equals("")
            ){
               if(dbHelper.query(((EditText) findViewById(R.id.SQLite_name)).getText().toString(),((EditText) findViewById(R.id.SQLite_phone)).getText().toString())){
                   ((TextView) findViewById(R.id.SQLite_result)).setText("查询成功\nusername:"+((EditText) findViewById(R.id.SQLite_name)).getText().toString()+"\nphone:"+((EditText) findViewById(R.id.SQLite_phone)).getText().toString());
               }else {
                   ((TextView) findViewById(R.id.SQLite_result)).setText("查询失败\nusername:"+((EditText) findViewById(R.id.SQLite_name)).getText().toString()+"\nphone:"+((EditText) findViewById(R.id.SQLite_phone)).getText().toString());
               }
            }else {
                Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
            }


        });
        ((Button) findViewById(R.id.SQLite_delete)).setOnClickListener(v -> {
            if(     !((EditText) findViewById(R.id.SQLite_name)).getText().toString().isEmpty() &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().isEmpty() ||
                    !((EditText) findViewById(R.id.SQLite_name)).getText().toString().equals("") &&
                    !((EditText) findViewById(R.id.SQLite_phone)).getText().toString().equals("")
            ){
               if(dbHelper.delete(((EditText) findViewById(R.id.SQLite_name)).getText().toString(),((EditText) findViewById(R.id.SQLite_phone)).getText().toString())){
                   ((TextView) findViewById(R.id.SQLite_result)).setText("删除成功\nusername:"+((EditText) findViewById(R.id.SQLite_name)).getText().toString()+"\nphone:"+((EditText) findViewById(R.id.SQLite_phone)).getText().toString());

               }else {
                   ((TextView) findViewById(R.id.SQLite_result)).setText("删除失败\nusername:"+((EditText) findViewById(R.id.SQLite_name)).getText().toString()+"\nphone:"+((EditText) findViewById(R.id.SQLite_phone)).getText().toString());

               }
            }else {
                Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
            }

        });
        ((Button) findViewById(R.id.SQLite_update)).setOnClickListener(v -> {
            // 根据姓名修改手机号
           String username_text=((EditText) findViewById(R.id.SQLite_name)).getText().toString();
           String phone_text=((EditText) findViewById(R.id.SQLite_phone)).getText().toString();
           if(!username_text.equals("")) {
               sqLiteDatabase = dbHelper.getWritableDatabase();
               ContentValues values = new ContentValues();
               values.put("phone", phone_text);
               int update = sqLiteDatabase.update("user", values, "name=?", new String[]{username_text});
               if (update > 0) {
                   ((TextView) findViewById(R.id.SQLite_result)).setText("修改成功\nusername:"+username_text+"\nphone:"+phone_text);
               }
               else {
                   ((TextView) findViewById(R.id.SQLite_result)).setText("修改失败\nusername:"+username_text+"\nphone:"+phone_text);
               }
           }{
               Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
            }
        });

    }

}